package core;

import static org.junit.Assert.*;
import static java.lang.Math.signum;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class BarcodeTest {
	
	static Barcode test1, test2, test3, test4, test5, test6;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		test1 = new Barcode("41234512345", BarcodeType.ITEM);
		test2 = new Barcode("812345123458", BarcodeType.PRODUCT);
		test3 = new Barcode("41234512346", BarcodeType.ITEM); //greater than 1
		test4 = new Barcode("812345123457", BarcodeType.PRODUCT); //less than 2
		test5 = new Barcode("41234512346", BarcodeType.ITEM); //duplicate of 3
		
		//1 3/5 4 2
	}

	@After
	public void tearDown() throws Exception {
		test1 = null;
		test2 = null;
		test3 = null;
		test4 = null;
		test5 = null;
	}

	@Test
	public void testBarcode() {
		Barcode test = new Barcode("41234512345", BarcodeType.ITEM);
		assertEquals(test.getString(), "412345123458");
		assertEquals(test.getType(), BarcodeType.ITEM);
	}

	@Test
	public void testGetString() {
		assertEquals(test1.getString(), "412345123458");
		assertEquals(test2.getString(), "812345123458");
		assertEquals(test3.getString(), "412345123465");
		assertEquals(test4.getString(), "812345123457");
	}

	@Test
	public void testGetType() {
		assertEquals(test1.getType(), BarcodeType.ITEM);
		assertEquals(test2.getType(), BarcodeType.PRODUCT);
		assertEquals(test3.getType(), BarcodeType.ITEM);
		assertEquals(test4.getType(), BarcodeType.PRODUCT);
	}

	@Test
	public void testGetNumberRepresentation() {
		assertEquals(test1.getNumberRepresentation(), 412345123458L);
		assertEquals(test3.getNumberRepresentation(), 412345123465L);
	}
	
	@Test
	public void testEquals() {
		//Wheeee~!
		assertTrue(test1.equals(test1));
		assertTrue(test2.equals(test2));
		assertTrue(test3.equals(test3));
		assertTrue(test4.equals(test4));
		assertTrue(test5.equals(test5));
		
		assertFalse(test1.equals(test2));
		assertFalse(test1.equals(test3));
		assertFalse(test1.equals(test4));
		assertFalse(test1.equals(test5));
		assertFalse(test2.equals(test1));
		assertFalse(test3.equals(test1));
		assertFalse(test4.equals(test1));
		assertFalse(test5.equals(test1));
		
		assertFalse(test2.equals(test3));
		assertFalse(test2.equals(test4));
		assertFalse(test2.equals(test5));
		assertFalse(test3.equals(test2));
		assertFalse(test4.equals(test2));
		assertFalse(test5.equals(test2));
		
		assertFalse(test3.equals(test4));
		assertTrue(test3.equals(test5));
		assertFalse(test4.equals(test3));
		assertTrue(test5.equals(test3));
		
		assertFalse(test4.equals(test5));
		assertFalse(test5.equals(test4));
	}
	
	@Test
	public void testCompareTo() {
		assertTrue(signum(test1.compareTo(test2)) == -signum(test2.compareTo(test1)));
		
		assertTrue(test3.compareTo(test5) == 0);
		assertTrue(signum(test3.compareTo(test1)) == signum(test5.compareTo(test1)));
		
		assertTrue(test2.compareTo(test4) > 0);
		assertTrue(test4.compareTo(test1) > 0);
		assertTrue(test2.compareTo(test1) > 0);	
	}

}
